﻿using Microsoft.AspNetCore.Mvc;
using OpenAdmin.Application;
using OpenAdmin.Application.Services.Dtos;
using OpenAdmin.Application.Services;
using System.Threading.Tasks;
using System.Collections.Generic;
using System;

namespace OpenAdmin.HttpApi.Controllers
{
    /// <summary>
    /// 权限相关Api
    /// </summary>
    [ApiExplorerSettings(GroupName = "Application")]
    public class PermissionController : ApiControllerBase
    {
        private readonly IPermissionService _permissionServices;
        /// <summary>
        /// 权限相关
        /// </summary>
        /// <param name="permissionServices"></param>
        public PermissionController(IPermissionService permissionServices)
        {
            _permissionServices = permissionServices;
        }

        /// <summary>
        /// 获取当前用户权限菜单
        /// </summary>
        /// <returns></returns>
        [HttpGet, ProducesResponseType(typeof(List<PermissionMenuResponseDto>), 200)]
        public async Task<IActionResult> GetUserPermissionMenus()
        {
            return Ok(await _permissionServices.GetUserPermissionMenus());
        }

        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, ProducesResponseType(typeof(List<PermissionResponseDto>), 200)]
        public async Task<IActionResult> GetPermissions()
        {
            return Ok(await _permissionServices.GetPermissions());
        }

        /// <summary>
        /// 获取指定用户权限
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [HttpGet, ProducesResponseType(typeof(List<UserPermissionResponseDto>), 200)]
        public async Task<IActionResult> GetUserPermissions(int userId)
        {
            return Ok(await _permissionServices.GetUserPermissions(userId));
        }

        /// <summary>
        /// 获取角色权限列表
        /// </summary>
        /// <returns></returns>
        [HttpGet, ProducesResponseType(typeof(RolePermissionResponseDto), 200)]
        public async Task<IActionResult> GetRolePermissions(int roleId)
        {
            return Ok(await _permissionServices.GetRolePermissions(roleId));
        }

        /// <summary>
        /// 获取租户权限列表
        /// </summary>
        /// <param name="tenantId"></param>
        /// <returns></returns>
        [HttpGet, ProducesResponseType(typeof(TenantPermissionResponseDto), 200)]
        public async Task<IActionResult> GetTenantPermissions(Guid tenantId)
        {
            return Ok(await _permissionServices.GetTenantPermissions(tenantId));
        }

        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SaveRolePermission([FromBody] SaveRolePermissionRequestDto requestDto)
        {
            await _permissionServices.SaveRolePermission(requestDto);

            return Ok();
        }

        /// <summary>
        /// 保存租户系统管理呗权限
        /// </summary>
        /// <param name="requestDto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> SaveTenantPermission([FromBody] SaveTenantPermissionRequestDto requestDto)
        {
            await _permissionServices.SaveTenantPermission(requestDto);

            return Ok();
        }
    }
}
